package com.sendpo.zjx.sendpo.utils;

import android.app.Application;
import android.support.annotation.Nullable;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;
import java.util.Map;

/**
 * ProjectName newswisdom
 * Author Administrator
 * Date 2016/12/8
 * Information
 */

public class Xutils {
    /**
     * 发送get请求
     */

   public static  <T>Callback.Cancelable GET(String url, Map<String,String> map, Callback.CommonCallback<T> callback){
       RequestParams params=new RequestParams(url);
            if(map!=null){
                  for(Map.Entry<String,String> entry:map.entrySet()){
                        params.addQueryStringParameter(entry.getKey(),entry.getValue());
                  }
            }
       params.setCharset("UTF-8");
       Callback.Cancelable cancelable= x.http().get(params,callback);
       return null;
   };

    /**
     * 发送post请求
     */
    public static  <T>Callback.Cancelable POST(String url, Map<String,String> map, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(map!=null){
            for(Map.Entry<String,String> entry:map.entrySet()){
                params.addQueryStringParameter(entry.getKey(),entry.getValue());
            }
        }
        params.setCharset("UTF-8");
        Callback.Cancelable cancelable= x.http().post(params,callback);
        return null;
    };

    //上传文件
  public static <T>Callback.Cancelable UpLoadFile(String url, Map<String,Object> map, Callback.CommonCallback<T> callback){
                     RequestParams params=new RequestParams(url);
                 if(map!=null){
                      for(Map.Entry<String,Object> entry:map.entrySet()){
                             params.addParameter(entry.getKey(),entry.getValue());
                      }
                 }
                    params.setCharset("UTF-8");
                   params.setMultipart(true);
                 Callback.Cancelable cancelable= x.http().post(params,callback);
      return  cancelable;
  }

//下载图片
    public static <T>Callback.Cancelable DownloadLoadFile(String url, String filesavepath, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        params.setSaveFilePath(filesavepath);
        params.setAutoResume(true);
        params.setCharset("UTF-8");
        Callback.Cancelable cancelable= x.http().get(params,callback);
        return  cancelable;
    }


//查询指定表里的所有数据
   @Nullable
   public static <T>List<T>  queryAll(Application application, Class<T> className){
            DbManager db= Tools.getDbManager(application, Tools.DBNAME, Tools.DBVERSION);
       try {
           List<T> list=db.findAll(className);
             if(list!=null&&list.size()!=0){
                 return  list;
             }
       } catch (DbException e) {
           e.printStackTrace();
       }
   return null;
   }


    //保存指定表里的数据

    public static  boolean save(Application application, Object  object){
        DbManager db= Tools.getDbManager(application, Tools.DBNAME, Tools.DBVERSION);
        try {
            db.save(object);
        } catch (DbException e) {
            e.printStackTrace();
        return false;
        }
    return true;
    }
    //更新指定表里的数据

    public static  boolean update(Application application, Object  object){
        DbManager db= Tools.getDbManager(application, Tools.DBNAME, Tools.DBVERSION);
        try {
            db.update(object);
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    //根据ID删除数据
     public static  <T>boolean deleteById(Application application,Integer id,Class<T> Class){
         DbManager db= Tools.getDbManager(application, Tools.DBNAME, Tools.DBVERSION);
         try {
             db.deleteById(Class,id);
         return true;
         } catch (DbException e) {
             e.printStackTrace();

         }
         return  false;
     }
    public static  <T>boolean DropTable(Application application,Class<T> Class){
        DbManager db = Tools.getDbManager(application, Tools.DBNAME, Tools.DBVERSION);
        try {
            db.dropTable(Class);
        } catch (DbException e) {
            e.printStackTrace();
        return  false;
        }
        return  true;
    }
}
